@echo off
chcp 65001 >nul
echo 🚀 Development Start - Unit Choice Quiz System
echo.

REM Kill existing processes to avoid port conflicts
echo 🔄 Stopping existing services...
taskkill /f /im node.exe >nul 2>&1
taskkill /f /im python.exe >nul 2>&1
timeout /t 1 /nobreak >nul

REM Check requirements
echo 🔍 Checking requirements...
python --version >nul 2>&1 || (echo ❌ Python not found & pause & exit /b 1)
node --version >nul 2>&1 || (echo ❌ Node.js not found & pause & exit /b 1)

echo ✅ Requirements check passed
echo.

REM Install dependencies quickly
echo 📦 Installing dependencies...
cd backend && pip install -r requirements.txt >nul 2>&1 && cd ..
cd frontend && npm install >nul 2>&1 && cd ..

echo ✅ Dependencies installed
echo.

REM Start services
echo 🚀 Starting backend server (port 8000)...
start "🐍 Backend Server" cmd /k "title Backend Server & color 0E & cd backend & echo Starting backend server... & echo Backend: http://localhost:8000 & echo Admin: http://localhost:8000/admin & echo. & python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload"

timeout /t 5 /nobreak >nul

echo 🚀 Starting frontend server (port 3000)...
start "⚛️ Frontend Server" cmd /k "title Frontend Server & color 0B & cd frontend & echo Starting frontend development server... & echo Frontend: http://localhost:3000 & echo Please wait for 'Local: http://localhost:3000' message... & echo. & npm run dev"

timeout /t 8 /nobreak >nul

echo.
echo ✅ Services started!
echo.
echo 🌐 Access URLs:
echo   📱 Frontend: http://localhost:3000
echo   🔧 Backend:  http://localhost:8000
echo   👨‍💼 Admin:    http://localhost:8000/admin
echo.
echo 📋 Two server windows opened:
echo   1. 🐍 Backend Server (Yellow)
echo   2. ⚛️ Frontend Server (Cyan)
echo.
echo 💡 Wait for "Local: http://localhost:3000" in Frontend window
echo.
echo Opening browser in 3 seconds...
timeout /t 3 /nobreak >nul
start http://localhost:3000

echo.
echo 🎉 Development environment ready!
echo 📝 This window can be closed safely.
pause
